package com.garen.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

public class DateUtils {
	
	private static String[] weeks = {"天","一","二","三","四","五","六"};
	
	public static String getWeekStr(Date date){
		if(date == null) return null;
		Calendar cale = Calendar.getInstance();
		cale.setTime(date);
		int w = cale.get(Calendar.DAY_OF_WEEK);
		return "星期" + weeks[w - 1];
	}
	/**
	 * 设置时间当天最大值
	 * @param date
	 * @return
	 */
	public static Date fullDate(Date date){
		Calendar cale = Calendar.getInstance();
		cale.setTime(date);
		fullCale(cale);
		return cale.getTime();
	}
		
	/**
	 * 设置时间当天最大值
	 * @param cal
	 * @return
	 */
	public static Calendar fullCale(Calendar cal){
		cal.set(Calendar.HOUR_OF_DAY,23);
		cal.set(Calendar.MINUTE,59);
		cal.set(Calendar.SECOND,59);
		cal.set(Calendar.MILLISECOND,999);
		return cal;
	}
	/**
	 * 清零 时分秒毫秒
	 * @param date
	 * @return
	 */
	public static Date clearDate(Date date){
		Calendar cale = Calendar.getInstance();
		cale.setTime(date);
		clearCale(cale);
		return cale.getTime();
	}
		
	
	/**
	 * 清零 时分秒毫秒
	 * @param cal
	 * @return
	 */
	public static Calendar clearCale(Calendar cal){
		//Calendar cal = Calendar.getInstance();
		//cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY,0);
		cal.set(Calendar.MINUTE,0);
		cal.set(Calendar.SECOND,0);
		cal.set(Calendar.MILLISECOND,0);
		return cal;
	}
		
	
	/**
	 * 日期格式为指定格式的字符串
	 * @param date 
	 * @param pattern 样式，如:yyyy-MM-dd HH:mm:ss
	 * @return
	 * @throws NullPointerException
	 */
	public static String date2str(Date date, String pattern)
			throws NullPointerException {
		if(date == null) return null;
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		String result = "";
		try {
			result = sdf.format(date);
		} catch (NullPointerException e) {
			throw e;
		}
		return result;
	}

	/**
	 * 字符串转换为日期 !
	 * @param date
	 * @param pattern 样式，如:yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static Date str2date(String date, String pattern){
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Date d  = null;
		if(StringUtils.isEmpty(date)) return null;
		try {
			d = sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}
	
	
}
